package com.MoveEat.activities.adapters;

import java.util.ArrayList;

import org.osmdroid.views.MapView;

import com.MoveEat.views.MenuHandler;
import com.MoveEat.views.RestMap;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.LinearLayout;

public class MoveEatViewGalleryAdapter extends BaseAdapter
{

	Context							mContext;
	int								mCount;
	private ArrayList<MenuHandler>	mList;
	private View					mRestView;
	private RestMap					mMapView;

	public MoveEatViewGalleryAdapter(Context context, int count, ArrayList<MenuHandler> list, View restView, RestMap mapView)
	{
		mContext = context;
		mCount = count;
		mList = list;
		mRestView = restView;
		mMapView = mapView;
		mMapView.getView().setEnabled(false);

	}

	@Override
	public int getCount()
	{
		return mCount + 2;
	}

	@Override
	public Object getItem(int pos)
	{

		if (pos >= 2)
		{
			View v = mList.get(pos - 2).getView();
			return v;
		}
		else if (pos == 1)
		{
			return mRestView;
		}
		else
		{
			return mMapView.getView();
		}
	}

	@Override
	public long getItemId(int position)
	{

		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent)
	{
		View view;
		if (convertView == null)
		{
			view = new View(mContext);
			view.setLayoutParams(new Gallery.LayoutParams(Gallery.LayoutParams.FILL_PARENT, Gallery.LayoutParams.WRAP_CONTENT));
			if (position >= 2)
			{
				mMapView.getView().setEnabled(false);
				view = mList.get(position - 2).getView();
			}
			else if (position == 1)
			{
				mMapView.getView().setEnabled(false);
				view = mRestView;
			}
			else
			{
				mMapView.getView().setEnabled(true);
				MapView mapView = (MapView) mMapView.getView();
				mapView.getController().setCenter(mMapView.getRestLocation());
				view = mMapView.getView();
			}
		}
		else
		{
			if (convertView instanceof MapView)
			{
				((MapView) convertView).getController().setCenter(mMapView.getRestLocation());
			}
			view = (LinearLayout) convertView;
		}
		return view;
	}

	public void setRestView(View restView)
	{
		mRestView = restView;
	}
}
